/* eslint-disable semi */
<template>
  <div>
  <div class="detail-container" v-if="showAll">
    <detail-header :detailAll="detailAll" @OrderUrl="OrderUrl"></detail-header>
    <div class="detail-con">
      <receiving-address :detailAll="detailAll"></receiving-address>
      <!-- 周期购 -->
      <cycle-info
        v-if="detail.is_cycle == 1 && (( detail.express && detail.express_no && detail.express_name) || (detailAll.cycle_send && detailAll.cycle_send.length>0))"
        :detailAll="detailAll"
      ></cycle-info>
      <!-- 商品信息 -->
      <goods-info :detailAll="detailAll"></goods-info>

      <!-- 结算信息 -->
      <money-info :detailAll="detailAll"></money-info>

      <!-- 虚拟帐号信息 -->
      <item-virtual
        v-if="detail.is_item_virtual == 1 && detail.item.length>0"
        :detailAll="detailAll"
        :dzData="dzData"
      ></item-virtual>

      <order-info :detail="detail" :detailAll="detailAll" @init="init"></order-info>
      <!-- 自提二维码 -->
      <order-code
          v-if="!detailAll.is_show_buy"
          :detailAll="detailAll"
        ></order-code>
      <order-log-info :order_verify_log="order_verify_log" />

     <dz-medical-treatment v-if="detailAll.is_prescription_drug" :detailAll="detailAll" />

      <div v-if="detail.item">
        <!-- 运单信息 -->
        <express-info
          v-if="detail.address_id != 0 && (detail.status == 1 || detail.status == 2 || detail.status == 3) && (detail.is_self_take == 0 ||(detail.shipping_type == 5 && detail.express_no)) && detail.is_cycle != 1"
          :detail="detail"
        ></express-info>

        <!-- 代付详情 -->
        <peer-pay
          v-if="detail.pay_type == 7"
          :detailAll="detailAll"
        ></peer-pay>

        <!-- 支付凭证 -->
        <upload-doc v-if="detail.pay_type == 10" :detailAll="detailAll"></upload-doc>

        <div class="item-block custom-pic" v-if="detailAll.custom_pic">
          <section class="title">上传的图片</section>
          <div class="img-box">
            <div class="img-item" v-for="(img,index) in detailAll.custom_pic" :key="index">
              <img :src="img.pic_url" class="card_img smallimg fl" />
            </div>
          </div>
        </div>

        <!-- 收货信息 -->
        <!-- <receiving-info :detail="detail" :otherData="receivingInfoData"></receiving-info> -->

        <!--门店配送地址-->
        <store-rider v-if="detail.shipping_type==5 && detail.self_take_address" :detail="detail"></store-rider>
        
        <!-- 周期购配送次数 -->
        <div class="item-block item-block2" v-if="detail.is_cycle == 1">
          <div class="title">周期购配送信息</div>
          <div class="line-con">
            <div class="label">配送次数：</div>
            <div class="line-detail">{{detail.item_cycle_type}}，共{{detail.item_cycle_sku}}期</div>
          </div>
        </div>
        <!-- 周期购配送次数end -->
        
        <!-- 发票信息 -->
        <invoice-info v-if="detailAll.invoice_info" :detailAll="detailAll"></invoice-info>
        <!-- 预售 -->
        <presaleInfo v-if="detail.is_presale == 1 && detail.status < 4 " :detail="detail"></presaleInfo>
        <!-- 商品附带图片信息 -->
        <img-goods :orderId="id" />

        <!-- 定制 源城好万家 条形码 start -->
        <div class="item-block item-block2 order-info-block" v-if="is_tsm_dz">
          <div class="title">条码信息</div>
          <div class="line-con" style="justify-content: center;margin: 30px 0;">
            <img :src="barCode" />
          </div>
        </div>
        <!-- 定制 源城好万家 条形码 end -->

        <!-- 按钮 问诊定制第一期不显示，但是第二期显示 -->
        <btn-info
          v-if="!detailAll.is_show_buy && (!detailAll.is_prescription_drug || (detailAll.is_prescription_drug && detailAll.is_need_consultation))"
          :detailAll="detailAll"
          :dzData="dzData"
          :queryData="queryData"
        ></btn-info>
      </div>
      <div class="item-block item-block2" v-if="detail.item && detail.item.length>0 && detailAll.gys">
        <div class="title">该包裹中商品不是该供应商商品</div>
      </div>
    </div>

    <!-- <pay-password :showPwd="showPwd" @closePwd="closePwd" @payPwdCallback="payPwdCallback"></pay-password> -->

    <!-- 付款弹窗 -->
   <!--  <van-popup v-model="showPayPop" class="pay-box" position="top" :style="{ height: '100%' }">
      <more-payment @closePayPop="closePayPop" @payCallback="payCallback" :payPopData="payPopData"></more-payment>
    </van-popup> -->
  </div>
  <div v-else>
    <no-data :noData=noData @click="loginCheck"></no-data>
  </div>
    <!-- 授权弹出框 -->
    <small-login ref="smalllogin"></small-login>
  </div>
</template>

<script>
import Vue from 'vue'
import {
  detailHeader,
  receivingAddress,
  cycleInfo,
  goodsInfo,
  orderInfo,
  itemVirtual,
  moneyInfo,
  expressInfo,
  peerPay,
  uploadDoc,
  storeRider,
  ReceivingInfo,
  invoiceInfo,
  presaleInfo,
  orderCode,
  btnInfo,
  dzMedicalTreatment,
  orderLogInfo
} from './components'
import { getOrderDetail, addUpdateOrderAddress } from '@/api/order/order.js'
import { openPage } from '@/utils/utils'
import small from '@/smallapp/small'
import imgGoods from '@/views/order/components/imgGoods'
import Store from '@/utils/store'
import SmallLogin from '@/components/SmallLogin/smallLogin'
import noData from '@/components/noData/index'

/*
import PayPassword from '@/components/PayPassword/index'
import MorePayment from '@/components/MorePayment/index' */
let consultation_url = ''
export default Vue.extend({
  data() {
    return {
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_order.png',
        text: '暂无订单',
        marginTop: '60px',
        width: '214px',
        txtMarginTop: '-46px'
        // txtColor: '#999'
      },
      dzData: {
        jxgzl_dz: false // 江西赣中旅定制
      },
      showAll: false,
      detailAll: {},
      detail: {},
      queryData: {},
      is_tsm_dz: 0, // 源城好万家 定制插件字段
      barCode: '',
      id: '',
      order_verify_log: []
      /*  showPwd: false, // 支付密码弹框是否显示
      showPayPop: false, // 付款弹窗

      payPopData: {},
      submitData: {},
      must_pay_password: '',
      popup_show: '' */
      /*  // 支付
      is_alipay: '',
      is_wx_pay: '',
      is_cod: '',
      is_bank: '',
      is_paypal: '',
      is_beibao: '',
      is_offline: '',
      is_han_te_pay: '',
      showPwd: false, // 支付密码弹框是否显示
      showPayPop: false, // 付款弹窗

      payPopData: {},
      submitData: {},
      must_pay_password: '',
      popup_show: '',
      // 头部
      is_delivery_supplier: '',
      supid: '',
      gys: '',
      // 周期购
      cycle_send: [
        {
          delivery_time: '',
          express_name: '',
          express_no: ''
        }
      ],
      // 商品列表
      groupBuy_num: '',
      groupBuy_left: '',
      jsapi_url: '',
      // 结算
      superior_dls_name: '',
      ftitle_coupon_name: '',
      coupon_code_alias: '',
      subtract_msg: '',
      taxes_alias: '',
      freight_insurance_name: '',
      freight_insurance: '',
      point_name: '',
      freight_payer4: null, // 到付
      order_detail: {
        pay_time: ''
      },
      // 代付
      peer_pay_list: [
        {
          nickname: '',
          pay_money: ''
        }
      ],
      peer_count: 0,
      peer_amount: 0,
      // 支付凭证
      imgList: [],
      // 收货信息
      take_their_name: '',
      // 发票信息
      invoice_info: {
        type_content: '',
        type: 0,
        invoice_header: '',
        invoice_no: '',
        company_address: '',
        company_phone: '',
        open_bank: '',
        bank_account: '',
        mail: '@',
        is_electronic: 0
      },
      // 按钮
      is_show_buy: null,
      is_dz_one: 0,
      is_price_share_title: '',
      store: '',
      new_store_name: '',
      is_confirm_receipt: '',
      has_duodian: '',
      is_duodian_model: '',
      Apply_for_after_sale: '',
      is_allow_return: '',
      is_allow_exchange: '',
      is_refund_config: 0,
      notime_pay: null,
      new_supplier_id: '',
      after_sale_tel: '',
      detail: {
        is_presale: 0, // 预售
        status_name: '', // 订单状态
        remarks: '',
        order_no: '', // 订单编号
        status: '', // 订单状态码
        create_time: '0000-00-00 00:00:00', // 创建时间
        end_time: '0000-00-00 00:00:00',

        store_reservation: {
          start_time: '',
          end_time: ''
        },

        delivery_supplier: [],
        order_id: '',
        refuse_reason: '', // 拒绝理由
        is_cycle: 0, // 周期购
        is_self_take: 0,
        express: '',
        express_no: '',
        express_name: '',
        store_gathering_id: '',
        payment: '',
        pay_type: null,
        coupon_repurchase_price: '', // 复购优惠
        is_item_virtual: null,
        coupon_fee: '', // 优惠券
        coupon_code_fee: '', // 优惠码
        first_remission_price: '', // 首单立减
        freight_collect_fee: '', // 运费
        post_fee: '',
        tax_fee: null, // 税费
        tax_invoice_fee: null, // 发票费
        point_fee: '', // 抵现
        virtual_currency_fee: '', // 虚拟币抵现
        address_id: '',
        shipping_type: 0,
        pay_image_list: [],
        self_take_address: '', // 门店配送地址
        name: '',
        mobile: '',
        address: '',
        card: '',
        card_img: '',
        email: '',
        message: '',
        receiver_name: '',
        receiver_mobile: '',
        self_address: '',
        one_status: '',
        desposit_payment: '',
        two_status: '',
        start_restpay_time: '',
        end_restpay_time: '',
        rest_payment: '',
        expand_money: '',
        real_payment: '',
        item_cycle_type: '',
        item_cycle_sku: '',
        is_dhs_virtual: '',
        self_type: '',
        need_comment: 0,
        is_share: 0,
        item: [
          {
            item_id: '',
            is_compress: null,
            pic_url: '',
            title: '',
            freight_payer: '',
            props: '',
            price: '',
            num: '',
            item_cycle_buy_id: '',
            order_extry: {
              item_date: '',
              item_memo: ''
            },
            item_config: {
              shopping_date_name: ''
            },

            message: [
              {
                account: null,
                pwd: null,
                msg: null
              }
            ]
          }
        ],
        dis_order_lists: [
          {
            express: '',
            express_no: '',
            express_name: '',
            id: '',
            type: 0,
            express_url: ''
          }
        ]
      } */
    }
  },
  beforeDestroy() {
    this.$Bus.$off('curEditAddress')
  },
  methods: {
    loginCheck() {
      if (!small.checkLogin()) {
        this.$refs.smalllogin.openSmallLoginPop()
      }
    },
    openLink(link) {
      openPage(link)
    },
    init() {
      let options = wx.getEnterOptionsSync();
      this.id = this.$route.query.id || options.query.id
      const queryData = {
        id: this.$route.query.id || options.query.id,
        page: this.$route.query.page || options.query.page,
        label_type: this.$route.query.label_type || options.query.label_type,

        // id: this.detailData.order_id,
        supid: this.$route.query.supid
      }
      this.queryData = queryData
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 10000)
      if(consultation_url) {
        consultation_url = decodeURIComponent(consultation_url)
        // console.log('456url', consultation_url)
        Store.saveLocal('webSrc', consultation_url)
        consultation_url = ''
        this.$JumpName(this, 'webView', {title: '处方药'})
        return
      }

      getOrderDetail(queryData).then((res) => {
        this.$loadingWrap.close()
        if (res.data.redirect_url) {
          this.openLink(res.data.redirect_url)
          return false
        }
        if (res.status == 1) {
          this.showAll = true
          this.detailAll = res.data
          this.detail = res.data.detail
          this.order_verify_log = res.data.detail.order_verify_log || []
          this.must_pay_password = res.data.must_pay_password
          this.popup_show = res.data.popup_show
          // 定制 源城好万家 条形码
          this.is_tsm_dz = res.data.is_tsm_dz ? res.data.is_tsm_dz : 0
          this.barCode = `${wx.getExtConfigSync().appApiUrl?wx.getExtConfigSync().appApiUrl:''}/Public/qrcode/text/${this.detailAll.detail.order_no}/sid/${this.$store.getters.sid}.html?pid=${this.$store.getters.pid}`
          // 十人团定制 隐藏退货按钮
          if (res.data.aggregation_type && res.data.aggregation_type == 6) {
            this.detailAll.istendz = true
          } else {
            this.detailAll.istendz = false
          }
          // this.cerateImgList() // 处理支付凭证图片
          const kfCard = {
            customCardId: this.detail.order_id,
            customCardStatus: this.detail.status_name,
            customCardCount: this.detail.buy_num,
            customCardCode: this.detail.order_no,
            customCardTime: this.detail.create_time,
            customCardName: this.detail.item[0].list[0].title,
            customCardThumbnail: (this.detail.item[0].list[0].pic_url).replace(/@!/g, ''),
            customCardAmount: this.detail.item[0].list[0].price,
            customCardDesc: this.detail.item[0].list[0].props,
          }
          this.$Bus.$emit('handleOrderChange', kfCard)
        } else {
          this.$Error(res.msg)
          this.noData.text = res.msg
        }
      })
    },
    is_dz() {
      // TODO 小程序使用全局的shopId
      let shop = small.globalData.shopId
      const is_jxgzl_dz = this.$_shop('$_jxgzl_dz', shop)
      this.dzData.jxgzl_dz = is_jxgzl_dz
    },
    /* cerateImgList() {
      const imgList = []
      if (this.detail.pay_image_list.length > 0) {
        this.detail.pay_image_list.map(val => {
          imgList.push({ url: val })
        })
      }
      this.imgList = imgList
    }, */
    // 分单
    OrderUrl(query) {
      this.init(query)
    }
  },
  watch: {
    $route: {
      handler(val, oldVal) {
        if (val.query.supid != oldVal.query.supid) {
          // this.init()
          // TODO 小程序使用init
            this.init()
            this.is_dz()
        }
      },
      // 深度观察监听
      deep: true
    }
  },
  computed: {
    item_vo() {
      let data = {}
      this.detail.item.map((val) => {
        return (data = val)
      })
      return data
    }

    /* // 头部数据
    headerData() {
      const data = {
        is_delivery_supplier: this.is_delivery_supplier,
        supid: this.supid,
        gys: this.gys
      }
      return data
    },
    // 周期购数据
    cycleInfoData() {
      const data = {
        cycle_send: this.cycle_send
      }
      return data
    },
    // 商品信息数据
    goodsInfoData() {
      const data = {
        groupBuy_num: this.groupBuy_num,
        groupBuy_left: this.groupBuy_left,
        jsapi_url: this.jsapi_url
      }
      return data
    },
    // 虚拟账号信息数据
    itemVirtualData() {
      const data = {}
      return data
    },
    // 结算信息数
    moneyInfoData() {
      const data = {
        superior_dls_name: this.superior_dls_name,
        item_vo: this.item_vo,
        ftitle_coupon_name: this.ftitle_coupon_name,
        coupon_code_alias: this.coupon_code_alias,
        subtract_msg: this.subtract_msg,
        taxes_alias: this.taxes_alias,
        freight_insurance: this.freight_insurance,
        freight_insurance_name: this.freight_insurance_name,
        point_name: this.point_name,
        freight_payer4: this.freight_payer4,
        order_detail: this.order_detail
      }
      return data
    },
    // 代付数据
    peerPayData() {
      const data = {
        peer_count: this.peer_count,
        peer_amount: this.peer_amount
      }
      return data
    },
    // 收货信息数据
    receivingInfoData() {
      const data = {
        take_their_name: this.take_their_name
      }
      return data
    },
    // 按钮数据
    btnInfoData() {
      const data = {
        is_dz_one: this.is_dz_one,
        is_price_share_title: this.is_price_share_title,
        store: this.store,
        new_store_name: this.new_store_name,
        is_confirm_receipt: this.is_confirm_receipt,
        has_duodian: this.has_duodian,
        is_duodian_model: this.is_duodian_model,
        Apply_for_after_sale: this.Apply_for_after_sale,
        is_allow_return: this.is_allow_return,
        is_allow_exchange: this.is_allow_exchange,
        notime_pay: this.notime_pay,
        after_sale_tel: this.after_sale_tel,
        new_supplier_id: this.new_supplier_id,
        is_refund_config: this.is_refund_config
      }
      return data
    } */
  },
  components: {
    orderLogInfo,
    imgGoods,
    detailHeader,
    receivingAddress,
    cycleInfo,
    goodsInfo,
    orderInfo,
    itemVirtual,
    moneyInfo,
    expressInfo,
    peerPay,
    uploadDoc,
    storeRider,
    ReceivingInfo,
    invoiceInfo,
    presaleInfo,
    orderCode,
    btnInfo,
    dzMedicalTreatment,
    SmallLogin,
    noData
  },
  created() {
    consultation_url = this.$route.query.consultation_url || ''
    this.is_dz()
  },
  mounted() {
    if (!small.checkLogin()) {
        this.$refs.smalllogin.openSmallLoginPop()
      }
      window.$$subscribe('loginReload', reload => {
        if (reload) {
          this.init()
        }
      })
      // TODO 订阅确认收货发出的广播
      window.$$subscribe('btnInfoRefresh', (data) => {
        // console.log('btnInfoRefresh',data)
        this.init()
        this.is_dz()
      })
      // TODO 订阅验证密码发出的广播
      window.$$subscribe('payPasswordRefresh', (data) => {
        // console.log('payPasswordRefresh', data)
        this.showPwd = false
        this.init()
      })
      // 订阅退换货申请发出的广播
      window.$$subscribe("refreshOrderList", () => {
        this.init()
      })
    window.addEventListener('wxshow', () => {
      // console.log('onshow')
      this.init()
    })
    this.$Bus.$on('curEditAddress', (res) => {
      console.log('res', res)
      if (res) {
        addUpdateOrderAddress({
          order_id: this.id,
          address_id: res.address_id
        }).then(res => {
          if (res.status == 1) {
            this.$Error('已提交审核')
            this.init()
          } else {
            this.$Error(res.msg)
          }
        })
        // that.init()
      }
    })
    // var that = this

    // this.$Bus.$on('curEditAddress', function(option) {
    //   console.log('option', option)
    //   if (option) {
    //     addUpdateOrderAddress({
    //       order_id: that.id,
    //       address_id: option.address_id
    //     }).then(res => {
    //       if (res.status == 1) {
    //         that.$Error('已提交审核')
    //         that.init()
    //       } else {
    //         that.$Error(res.msg)
    //       }
    //     })
    //     // that.init()
    //   }
    // })
  },
  beforeDestroy() {
    // TODO 取消订阅
      window.$$unsubscribe('btnInfoRefresh')
      window.$$unsubscribe('payPasswordRefresh')
      window.$$unsubscribe('refreshOrderList')
  }
})
</script>
<style lang="scss">
@import "src/styles/order/detail";
.miniprogram-root {
  // TODO 底部copyright样式处理
  .public-bottom {
    padding-bottom: 122px!important;
  }
}
//自定义上传图片
.custom-pic{
  .title{
    margin-bottom:20px;
  }
  .img-box{
    display: flex;
    flex-wrap: wrap;
  }
  .img-item{
    width:146px;
    height:146px;
    margin:16px 16px 16px 0;
    border-radius:2px;
    img{
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
}
</style>
